// packageE/goods/goods.js
var location = require("../../../mybehaviors/location/location");
var app = getApp();
Component({
  behaviors: [location],
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    bottomShowBolIndex: {
      type: null,
    },
    index: {
      type: null,
    },
    U_tabcontrol: {
      type: null,
    },
    bottomShowBol: {
      type: null,
    },
    isLast: {
      type: null,
    },
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage:  `${app.globalData.imgPath}image.png`,
    clientWidth: "375",
    list: [],
    language: "",
    goodsShow: true,

    page: 1,
    total_page: 0,
    isLoadMore: true,
    allLoaded: false, // 全部数据已经加载完成
    imgPath: app.globalData.imgPath,
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      let language = wx.getStorageSync("langIndex");
      let basic_info = wx.getStorageSync("yz_basic_info");
      if (basic_info.lang) {
        let goodsDiy = basic_info.lang.goods;
        let vip_price_txt = goodsDiy && goodsDiy.vip_price ? goodsDiy.vip_price : '会员价';
        this.setData({
          vip_price_txt
        })
      }
      this.setData({
        language: language.en,
        pointDiy : wx.getStorageSync('integral'),
        plugin_setting: basic_info.plugin_setting
      });
    },
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this.setData({
      clientWidth: wx.getSystemInfoSync().windowWidth,
      list: this.data.datas.list.data ?
        this.data.datas.list.data : this.data.datas.list,
    });

    this._getLocation((res, e) => {
      let point = app.qqMapTransBMap(parseFloat(e.lng),parseFloat(e.lat));
      this.setData({
        lat: point.lat,
        lng: point.lng,
        point: point
      });
      this.getdata();
    });
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    gotoDetail(e) {
      let id = e.currentTarget.dataset.item.goodid;
      wx.navigateTo({
        url: "/packageA/detail_v2/detail_v2?id=" + id,
      });
    },
    // 获取更多数据
    getMoreData() {
      let that = this;
      that.setData({
        isLoadMore: false,
      });
      if (that.data.page >= that.data.last_page) {
        return;
      } else {
        that.setData({
          page: that.data.page + 1,
        });
        // let ressa = wx.getStorageSync("address-point");
        let urlStr = app.getNetAddresss(
          "plugin.nearby-store-goods.frontend.controllers.designer.get-goods"
        );
        // let point = app.qqMapTransBMap(
        //   parseFloat(ressa.point.lng),
        //   parseFloat(ressa.point.lat)
        // );
        urlStr += "&page=" + that.data.page;
        urlStr += "&lng=" + that.data.point.lng;
        urlStr += "&lat=" + that.data.point.lat;
        urlStr += "&city_name=" + that.data.city;
        app._getNetWork({
          url: urlStr,
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              that.setData({
                last_page: res.data.last_page,
              });

              if (that.data.page < that.data.last_page) {
                that.setData({
                  isLoadMore: true,
                });
              } else {
                that.setData({
                  allLoaded: true,
                });
              }

              let _total = res.data.data.length + this.data.list.length;
              let slicedArray = res.data.data;
              if(this.data.datas.get_number > 0 && _total > this.data.datas.get_number) {
                slicedArray = res.data.data.slice(0, this.data.datas.get_number - this.data.list.length);
                this.data.isLoadMore = false;
                this.data.allLoaded = true;
              }
              this.data.list = this.data.list.concat(slicedArray);
              that.setData({
                list: this.data.list,
              });
            } else {
              that.setData({
                page: that.data.page - 1,
                isLoadMore: false,
                allLoaded: true,
              });
            }
          },
          fail: function (res) {
            console.log(res);
          },
        });
      }
    },
    getdata() {
      var that = this;
      // let ressa = wx.getStorageSync("address-point");
      // let point = {};
      let urlStr = app.getNetAddresss(
        "plugin.nearby-store-goods.frontend.controllers.designer.get-goods"
      );
      // if (ressa) {
      //   point = app.qqMapTransBMap(
      //     parseFloat(ressa.point.lng),
      //     parseFloat(ressa.point.lat)
      //   );
      // } else {
      //   point = {
      //     lng: "",
      //     lat: ""
      //   };
      // }

      urlStr += "&page=" + that.data.page;
      urlStr += "&lng=" + that.data.point.lng;
      urlStr += "&lat=" + that.data.point.lat;
      urlStr += "&city_name=" + that.data.city;
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              imgPath: app.globalData.imgPath,
              isLoadMore: false,
              last_page: res.data.last_page,
            });
            if (res.data.current_page < res.data.last_page) {
              that.setData({
                isLoadMore: true,
              });
            } else {
              that.setData({
                allLoaded: true,
              });
            }

            if(res.data.data.length > this.data.datas.get_number) {
            	res.data.data.splice(this.data.datas.get_number, res.data.data.length-this.data.datas.get_number);
            	that.setData({
            		isLoadMore: false,
            		allLoaded: true
            	})
            }

            that.setData({
              list: res.data.data,
            });
            // 跟无商品的时候图片冲突了
            // if (res.data.data <= 0) {
            //   that.setData({
            //     goodsShow: false,
            //   });
            // }
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false,
              allLoaded: true,
            });
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    },
  },
  observers: {
    bottomShowBol: function (flag) {
      if (flag && this.data.bottomShowBolIndex == this.data.index && this.data.datas.sort_style.choose_icon != 5 && this.data.isLoadMore) {
        this.getMoreData();
      }
    },
  },
});